package org.spring.aop.framework.adapter;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.spring.aop.BeforeAdvice;
import org.spring.aop.MethodBeforeAdvice;
import org.tool.core.lang.Assert;

import java.io.Serializable;

/**
 * MethodBeforeAdviceInterceptor
 *
 * @author 李坤
 * @date 2022/4/19 14:21
 */
public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeAdvice, Serializable {

    /**
     * 方法前置通知
     */
    private final MethodBeforeAdvice beforeAdvice;

    public MethodBeforeAdviceInterceptor(MethodBeforeAdvice beforeAdvice) {
        Assert.notNull(beforeAdvice, "MethodBeforeAdvice must not be null");

        this.beforeAdvice = beforeAdvice;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // 执行方法前置通知
        beforeAdvice.before(invocation.getThis(), invocation.getMethod(), invocation.getArguments());
        // 执行代理方法
        return invocation.proceed();
    }

}
